//
//  CityViewController.m
//  中国休闲
//
//  Created by Demo on 13-8-2.
//  Copyright (c) 2013年 usetc. All rights reserved.
//

#import "CityViewController.h"

#ifndef _____myDefine_h
#define _____myDefine_h
#define imageWithPath(name,type) ([UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:type]])


#endif

@interface CityViewController ()
{
    UITextField *searchTextField;
    UIButton *clearSearchButton;
    NSDictionary *dataDic;
    NSArray *titleArray;
    UILabel *currentLab;
    UIView *swipeView;
}
@end

@implementation CityViewController
@synthesize locManager;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)dealloc{
    [dataDic release];
    [titleArray release];
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	self.title=@"城市定位";
     self.view.backgroundColor=[UIColor colorWithRed:234.0/255 green:239.0/255 blue:245.0/255 alpha:1];
    //添加搜索框灰色背景
    UIImageView *searchBgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    searchBgView.image=imageWithPath(@"search_bj", @"png");
    [self.view addSubview:searchBgView];
    [searchBgView release];
    //添加搜索框
    UIImage  *searchBorImage=imageWithPath(@"search box", @"png");
    UIImageView *searchBorImageView=[[UIImageView alloc] initWithFrame:CGRectMake(20, 5, 280, 280*searchBorImage.size.height/searchBorImage.size.width)];
    searchBorImageView.image=searchBorImage;
    [self.view addSubview:searchBorImageView];
    [searchBorImageView release];
    
    searchTextField=[[UITextField alloc] initWithFrame:CGRectMake(55, 5, 240, 26)];
    [searchTextField setReturnKeyType:UIReturnKeySearch];
    
    searchTextField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
    searchTextField.placeholder=@"输入城市名搜索";
    searchTextField.font=[UIFont systemFontOfSize:15];
    [searchTextField addTarget:self action:@selector(searchTextBeginEdit) forControlEvents:UIControlEventEditingDidBegin];
    [searchTextField addTarget:self action:@selector(searchTextDidEdit) forControlEvents:UIControlEventEditingDidEndOnExit];
    [searchTextField addTarget:self action:@selector(searchTextValueChange) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:searchTextField];
    [searchTextField release];
    
    //添加搜索按钮
    UIButton *searchButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [searchButton setFrame:CGRectMake(26, 9, 17, 17)];
    
    [searchButton setBackgroundImage:imageWithPath(@"search", @"png") forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(searchTextDidEdit) forControlEvents:UIControlEventTouchUpInside];
    [searchButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [self.view addSubview:searchButton];
    
    //添加清除搜索框内容按钮
    clearSearchButton=[[UIButton alloc] initWithFrame:CGRectMake(275, 8, 20, 20)];
    [clearSearchButton setBackgroundImage:imageWithPath(@"close", @"png") forState:UIControlStateNormal];
    clearSearchButton.alpha=0;
    [clearSearchButton addTarget:self action:@selector(clearSearchButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:clearSearchButton];
    [clearSearchButton release];
    //添加位置列表
    UITableView *cityTableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 40, 320, [[UIScreen mainScreen] bounds].size.height-44-20-40)];
    cityTableView.delegate=self;
    cityTableView.dataSource=self;
    cityTableView.backgroundColor=[UIColor clearColor];
    [self.view addSubview:cityTableView];
    [cityTableView release];
    
    
    swipeView=[[UIView alloc] initWithFrame:CGRectMake(0, 40, 320, [[UIScreen mainScreen] bounds].size.height-44-20-40)];
    swipeView.alpha=0;
    swipeView.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [self.view addSubview:swipeView];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSwipe)];
    [swipeView addGestureRecognizer:tap];
    [tap release];
    
    
    dataDic=[[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"address" ofType:@"plist"]];
    titleArray=[[NSArray alloc] initWithObjects:@"当前定位",@"热门城市",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"J",@"K",@"L",@"M",@"N",@"P",@"Q",@"R",@"S",@"T",@"W",@"X",@"Y",@"Z", nil];
    [self findMe];
}
-(void)tapSwipe
{
    [searchTextField resignFirstResponder];
    swipeView.alpha=0;
}
-(void)searchTextBeginEdit
{
    swipeView.alpha=1;
    
}
-(void)searchTextValueChange
{
    if(searchTextField.text.length==0){
        clearSearchButton.alpha=0;
    }else{
        clearSearchButton.alpha=1;
    }
}
//跳转到搜索结果
-(void)searchTextDidEdit
{
    if(searchTextField.text.length==0){
        return;
    }
    swipeView.center=CGPointMake(160, swipeView.center.y-40);
    swipeView.alpha=0;
    swipeView.backgroundColor=[UIColor clearColor];
    [searchTextField resignFirstResponder];
    
    searchTextField.text=@"";
    
}
-(void)clearSearchButtonClick
{
    clearSearchButton.alpha=0;
    searchTextField.text=@"";
}

//查找自己的地点
-(void)findMe
{
    self.locManager = [[[CLLocationManager alloc] init] autorelease];
    locManager.delegate = self;
    locManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locManager startUpdatingLocation];
    locManager.distanceFilter = 1000.0f;
}
- (void)locationManager:(CLLocationManager *)manager
	 didUpdateLocations:(NSArray *)locations
{
    CLLocation *newLocation=[locations objectAtIndex:0];
    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
    [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        for (CLPlacemark * placemark in placemarks) {
            NSString *cityNmae = [NSString stringWithFormat:@"%@",placemark.locality];
            [currentLab setText:[cityNmae substringWithRange:NSMakeRange(0, cityNmae.length-1)]];
            NSUserDefaults *myUserDefaults=[NSUserDefaults standardUserDefaults];
            [myUserDefaults setValue:[cityNmae substringWithRange:NSMakeRange(0, cityNmae.length-1)] forKey:@"currentCity"];
            [myUserDefaults synchronize];
            break;
        }
    }];
    [locManager stopUpdatingLocation];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section==0){
        return 1;
    }
    return [[dataDic objectForKey:[titleArray objectAtIndex:section]] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *Cell;
    if(indexPath.section==0){
        Cell=@"currentCell";
    }else{
        Cell=@"Cell";
    }
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:Cell];
    if(cell==nil){
        cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cell] autorelease];
        if(indexPath.section==0){
            currentLab=[[UILabel alloc] initWithFrame:CGRectMake(15, 5, 80, 30)];
            currentLab.text=@"正在定位";
            currentLab.font=[UIFont fontWithName:@"ZhunYuan" size:17];
            currentLab.textColor=[UIColor colorWithRed:28.0/255 green:28.0/255 blue:28.0/255 alpha:1];
            currentLab.backgroundColor=[UIColor clearColor];
            [cell.contentView addSubview:currentLab];
            [currentLab release];
            
            UILabel *gpsLab=[[UILabel alloc] initWithFrame:CGRectMake(95, 10, 100, 20)];
            gpsLab.text=@"GPS定位城市";
            gpsLab.textColor=[UIColor colorWithRed:68.0/255  green:68.0/255  blue:68.0/255  alpha:1];
            gpsLab.font=[UIFont fontWithName:@"ZhunYuan" size:12];
            gpsLab.backgroundColor=[UIColor clearColor];
            [cell.contentView addSubview:gpsLab];
            [gpsLab release];
        }
    }
    if(indexPath.section==0){
    }else{
        NSArray *a=[dataDic objectForKey:[titleArray objectAtIndex:indexPath.section]];
        cell.textLabel.text=[a objectAtIndex:indexPath.row];
        cell.textLabel.font=[UIFont fontWithName:@"ZhunYuan" size:17];
    }
    return cell;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    
    return @[@"热门",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"J",@"K",@"L",@"M",@"N",@"P",@"Q",@"R",@"S",@"T",@"W",@"X",@"Y",@"Z"];
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUserDefaults *myUserDefaults=[NSUserDefaults standardUserDefaults];
    if(indexPath.section==0){
        [myUserDefaults setValue:[myUserDefaults valueForKey:@"currentCity"] forKey:@"myCity"];
        [myUserDefaults synchronize];
    }else{
        NSArray *a=[dataDic objectForKey:[titleArray objectAtIndex:indexPath.section]];
   
        [myUserDefaults setValue:[a objectAtIndex:indexPath.row] forKey:@"myCity"];
        [myUserDefaults synchronize];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [titleArray count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(section==0){
        return nil;
    }
    return [titleArray objectAtIndex:section];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
